const Controller = require("egg").Controller

class VideoController extends Controller{

  // 视频列表的获取
  async getList(){
    let {ctx} = this,body = ctx.request.body
    try {
      ctx.validate({
        page:{type:"number",require:true,default:1},
        pageSize:{type:"number",require:true,max:30,min:5},
      })


      // 编写service层
      let videoModel = await this.ctx.service.video.getList(body.page,body.pageSize)



      

      const userModel = this.ctx.model.User
      const temp = this.ctx.model.Video

      userModel.hasMany(temp);
      temp.belongsTo(userModel);


      let offset = parseInt((body.page-1)*body.pageSize)

      const result = await temp.findAndCountAll(
        { 
          include: userModel,
          offset: offset, 
          limit: body.pageSize ,
        }
        )

      //console.log("使用Sequelize获取的数据是",result)
    
      ctx.body = { msg:"success",code:0, total:result.count,data:result.rows,};
    } catch (error) {
      console.log(err)
      ctx.body = { success: false,code:500};
      return
    }
  }
  // 视频详情的获取
  async getInfo(){
    const { ctx } = this,params = ctx.params
    try {
      ctx.validate({
        id:{type:"string",require:true,default:"0"},
      },params)
      let data = await this.ctx.service.video.getInfo(params.id)
      ctx.body = { msg:"success",code:0, data};
    } catch (err) {
      console.log(err)
      ctx.body = { success: false,code:500};
      return;
    }
  }
}

module.exports = VideoController